﻿using AMAP.Areas.Client.Models.Order;
using AMAP.Domain;
using AMAP.ServiceInterfaces;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace AMAP.Areas.Client.Controllers
{
    public class OrderController : Controller
    {
        private IBasketOrderService BasketOrderService;
        private IBasketService BasketService;
        private IClientService ClientService;
        private IQuarterService QuarterService;
        private ISubscriptionService SubscriptionService;

        public OrderController(IBasketOrderService BasketOrderService, IBasketService BasketService, IClientService ClientService, IQuarterService QuarterService, ISubscriptionService SubscriptionService)
        {
            this.BasketOrderService = BasketOrderService;
            this.BasketService = BasketService;
            this.ClientService = ClientService;
            this.QuarterService = QuarterService;
            this.SubscriptionService = SubscriptionService;
        }

        //
        // GET: /Client/Order/
        public ActionResult Index()
        {
            return RedirectToAction("Create");
        }

        [Authorize(Roles = "Client")]
        public ActionResult Create()
        {
            ClientOrderCreateViewModel Model = new ClientOrderCreateViewModel();
            int ClientId = ClientService.GetByEmail(User.Identity.Name).Id;

            Model.LastBasketOrder = BasketOrderService.GetLast(ClientId);
            Model.LastBasketOrder = BasketOrderService.GetLast(ClientId);
            Model.DeliveryLocation = ClientService.GetByEmail(User.Identity.Name).Place;

            return View(Model);
        }

        [Authorize(Roles = "Client")]
        public ActionResult Subscribe()
        {
            ClientOrderSubscribeViewModel Model = new ClientOrderSubscribeViewModel();

            IList tmp = new List<Object>();
            tmp.Add(new { Text = "Sélectionnez", Value = "" });
            foreach (var e in this.QuarterService.GetAllAsList().Where(p => p.Index != 3))
                tmp.Add(new { Text = e.Year + " - " + "Trimestre " + e.Index + " (" + e.Price.ToEuro() + ")", Value = e.Id.ToString() });
            Model.QuarterList = new SelectList(tmp, "Value", "Text");

            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "Client")]
        public ActionResult Subscribe(ClientOrderSubscribeViewModel Model)
        {
            if (ModelState.IsValid)
            {
                Subscription Subscription = new Subscription
                {
                    ClientId = this.ClientService.GetByEmail(User.Identity.Name).Id,
                    Quantity = Model.Quantity,
                    QuarterId = Model.QuarterId
                };
                this.SubscriptionService.Add(Subscription);

                EmailService.Send("florent.henry@gmail.com", "Votre abonnement a été enregistré", "Félicitation Florent, <br> votre abonnement a été enregistré avec succès.<br><br>Préparez le paiement pour la première livraison.");

                return RedirectToRoute("Client_default", new { controller = "Home", action = "Index" });
            }

            IList tmp = new List<Object>();
            tmp.Add(new { Text = "Sélectionnez", Value = "" });
            foreach (var e in this.QuarterService.GetAllAsList().Where(p => p.Index != 3))
                tmp.Add(new { Text = e.Year + " - " + "Trimestre " + e.Index + " (" + e.Price.ToEuro() + ")", Value = e.Id.ToString() });
            Model.QuarterList = new SelectList(tmp, "Value", "Text");

            return View(Model);
        }

        [Authorize(Roles = "Client")]
        public ActionResult Basket()
        {
            ClientOrderBasketViewModel Model = new ClientOrderBasketViewModel();

            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "Client")]
        public ActionResult Basket(ClientOrderBasketViewModel Model)
        {
            if (ModelState.IsValid)
            {
                DateTime DeliveryDate;
                if (Model.DeliveryDate.HasValue)
                    DeliveryDate = Model.DeliveryDate.Value;
                else
                    DeliveryDate = DateTime.Now;

                BasketOrder Basket = new BasketOrder
                {
                    ClientId = this.ClientService.GetByEmail(User.Identity.Name).Id,
                    Quantity = Model.Quantity,
                    BasketId = this.BasketService.GetAllAsList().Last().Id
                };
                this.BasketOrderService.Add(Basket);

                return RedirectToRoute("Client_default", new { controller = "Home", action = "Index" });
            }
            return View(Model);
        }
    }
}
